package com.example.module_service.merchant

import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_service.R
import com.example.module_service.adapter.MerchantAdapter
import com.example.module_service.databinding.ActivityMerchantBinding
import com.example.module_service.vm.MerchantViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * //企业档案
 */
@Route(path = "/module_service/MerchantActivity")
@AndroidEntryPoint
class MerchantActivity : BaseMvvmActivity<ActivityMerchantBinding>() {
    val merchantAdapter=MerchantAdapter()

    private val merchantViewModel:MerchantViewModel by viewModels()

    override fun getLayoutId(): Int =R.layout.activity_merchant

    override fun initViews() {
        val editText = findViewById<EditText>(R.id.search_edit_text)
        editText.setBackgroundColor(Color.TRANSPARENT)

        binding.merchantRv.adapter=merchantAdapter
        binding.merchantRv.layoutManager=LinearLayoutManager(this)
        merchantViewModel.getListData()

        merchantViewModel.mSuccessLiveData.observe(this){
            merchantAdapter.data.addAll(it.data)
            merchantAdapter.notifyDataSetChanged()
        }

        merchantViewModel.mErrorLiveData.observe(this){errorMsg->
            Toast.makeText(this,errorMsg, Toast.LENGTH_SHORT).show()
        }

        merchantAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/module_service/MerchantDetailActivity")
                .withSerializable("merchant",merchantAdapter.data[position])
                .navigation()
        }

        //跳转入驻申请
        binding.moneyManage.setOnClickListener {
            ARouter.getInstance().build("/module_service/SettleActivity").navigation()
        }

        //搜索
        binding.searchEditText.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                merchantViewModel.mSuccessLiveData.observe(this@MerchantActivity){
                    merchantAdapter.data.clear()
                    merchantAdapter.data.addAll(it.data.sortedByDescending { it.id }.filter { it.name.contains(p0.toString()) })
                    merchantAdapter.notifyDataSetChanged()
                }
            }

            override fun afterTextChanged(p0: Editable?) {

            }

        })


    }

    override fun getActivityState(state: Bundle) {
        TODO("Not yet implemented")
    }

    override fun saveActivityState(outState: Bundle) {
        TODO("Not yet implemented")
    }

}